# -*- coding: UTF-8 -*-
# Author: Slp
# @Date : 2021-08-16
from src.UItest.cwt_ui_login import LoginTest
from src.UItest.cwt_ui_project import ProjectTest
from src.common.BeautifulReport import BeautifulReport

import time
import unittest
import os
import sys

from src.common.dingding import send_link
from src.common.file_upload import FileUpload

sss = {}
sep = os.sep
root_path = os.path.abspath(os.path.join(__file__, f"..{sep}..{sep}.."))
sys.path.append(root_path)


def runCase():
    try:
        now_time = time.strftime("%Y-%m-%d %H_%M_%S")
        # 判断文件夹时候存在
        if os.path.exists(r"../../testresult/") is False:
            os.mkdir(r"../../testresult/")
        # 定义路径的时候需要治愈结尾的/添加
        reportDir = (r"../../testresult/")
        reportFilename = now_time + '_result.html'
        # reportFilepath = reportDir + reportFilename
        # fp = open(reportFilepath, 'wb')
        ts = unittest.TestSuite()  # 实例化
        # 批量加入每个模块下的所有测试方法，添加对应测试模块下的类名即可，需要先引入模块
        ls = [unittest.defaultTestLoader.loadTestsFromTestCase(LoginTest)]

        ts.addTests(ls)  # 按函数加载测试用例
        result = BeautifulReport(ts)  # 加载执行用例生成报告
        result.report(description='车位通UI自动化测试', filename=reportFilename, report_dir=reportDir)  # 定义报告的属性

        # fp.close()

    except Exception as ex:
        print(str(ex))
    finally:
        # 本地调试推送测试报告使用路径
        fileUpload = FileUpload()
        fileUpload.qiNiu(reportFilename)  # 测试报告上传七牛
        time.sleep(5)
        send_link("测试报告：", "点击查看本次所有项目的测试用例执行详情", "http://ra808hcqs.hn-bkt.clouddn.com/" + reportFilename)

        # 最终提交后推送测试报告使用路径
        # send_link("测试报告", "点击查看本次所有项目的测试用例执行详情", "http://10.0.6.56:8068/Online-Inspection/" + filename)
        print("执行完毕")


if __name__ == '__main__':
    runCase()
